#makefile for sgu
#make [shared] [static]
#make userinstall
#make install
#make test
#make doc
#make doc-userinstall

include CONFIGME.mak
include filelist.mak

ifneq ($(shell uname | grep -i linux),)
	PLATFORM := LINUX
endif

ifneq ($(shell uname | grep -i freebsd),)
	PLATFORM := FREEBSD
endif

ifneq ($(shell uname | grep -i cygwin),)
	PLATFORM := CYGWIN
endif

ifeq ($(BUILDDIR),)
	BUILDDIR := $(HOME)/tmp
endif

ifeq ($(LOCALDIR),)
	LOCALDIR := /usr/local
endif

ifeq ($(USERLOCALDIR),)
	USERLOCALDIR := $(HOME)/local
endif


WARNINGS := \
	-Wall \
	-W \
	-Wpointer-arith \
	-Wcast-qual \
	-Wcast-align \
	-Wwrite-strings \
	-Wconversion \
	-Wsign-compare \
	-Woverloaded-virtual 


TIMESTAMP := $(shell date -u +%Y_%m_%d_%H_%M_%S_%N) 

CPPFLAGS :=
CXXFLAGS := --pipe -ffor-scope
CXXFLAGS += $(WARNINGS)
CPPFLAGS += -DSGU_TIMESTAMP=$(TIMESTAMP)
CPPFLAGS += -DSGU_PLATFORM=$(PLATFORM)
CPPFLAGS += -DSGU_VERSION=$(VERSION)
CPPFLAGS += -DSGU_RELEASE=$(RELEASE)
CPPFLAGS += -DSGU_BUILD

LDFLAGS := --pipe
LDLIBS :=


ifeq ($(DEBUGBUILD),1)
	CXXFLAGS += -ggdb
	LIBRARY := libsgu-dbg
	OBJPREFIX := $(BUILDDIR)/sgu/dbg
else
	CPPFLAGS += -DNDEBUG
	CXXFLAGS += -O3
	LIBRARY := libsgu
	OBJPREFIX := $(BUILDDIR)/sgu/rls
	LDFLAGS += -s
endif


OBJECTS:=$(addprefix $(OBJPREFIX)/,$(OBJECTS))


ifeq ($(PLATFORM),LINUX)
	SONAME := $(LIBRARY).so
	SONAMEV := $(SONAME).$(VERSION)
	SONAMEVR := $(SONAMEV).$(RELEASE)
	SOTARGET := $(OBJPREFIX)/$(SONAMEVR)
	ANAME := $(LIBRARY)-static.$(VERSION).a
	CXXFLAGS += -fpic
	LDFLAGS += -fpic
	WARNINGS += -Wshadow
endif

ifeq ($(PLATFORM),FREEBSD)
	SONAME := $(LIBRARY).so
	SONAMEV := $(SONAME).$(VERSION)
	SONAMEVR := $(SONAMEV).$(RELEASE)
	SOTARGET := $(OBJPREFIX)/$(SONAMEVR)
	ANAME := $(LIBRARY)-static.$(VERSION).a
	CXXFLAGS += -fpic
	LDFLAGS += -fpic
endif

ifeq ($(PLATFORM),CYGWIN)
	SONAME := $(LIBRARY).dll
	SONAMEV := $(LIBRARY).$(VERSION).dll
	SONAMEVR := $(LIBRARY).$(VERSION).$(RELEASE).dll
	SOTARGET := $(OBJPREFIX)/$(SONAMEVR)
	ANAME := $(LIBRARY)-static.$(VERSION).a
	LDFLAGS += -Wl,--export-all-symbols
	WARNINGS += -Wshadow
endif	


export SONAME
export SONAMEV
export SONAMEVR
export SOTARGET

export ANAME
export ATARGET := $(OBJPREFIX)/$(ANAME)

export VERSION
export HEADERS

.PHONY: init
.PHONY: install
.PHONY: userinstall
.PHONY: clean

all: lib 

lib: shared static

init:

ifndef PLATFORM
	$(error Unknown platform)
endif

	touch sgu.cc
	mkdir -p $(OBJPREFIX)


$(OBJPREFIX)/%.o : %.cc $(HEADERS)
	$(CXX) -shared -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

shared: init $(SOTARGET)
static: init $(ATARGET)

$(SOTARGET) : $(OBJECTS) $(OBJPREFIX)/sgu_dynamic.o
	$(CXX) $(LDFLAGS) -shared -o $@ -Wl,-soname=$(SONAMEV) $^ $(LDLIBS)

$(ATARGET) : $(OBJECTS) $(OBJPREFIX)/sgu_static.o
	$(RM) $(ATARGET)
	$(AR) cq $(ATARGET) $^
	$(AR) s $(ATARGET)

install-shared: shared
	./install-shared.sh $(LOCALDIR)
	ldconfig -v | grep $(BASENAME)

userinstall-shared: shared
	./install-shared.sh $(USERLOCALDIR)

install-static: static
	./install-static.sh $(LOCALDIR)

userinstall-static: static
	./install-static.sh $(USERLOCALDIR)

install-headers:
	./install-headers.sh $(LOCALDIR)

userinstall-headers: 
	./install-headers.sh $(USERLOCALDIR)

install: install-shared install-static install-headers
userinstall: userinstall-shared userinstall-static userinstall-headers

test: userinstall
	$(MAKE) -C test

doc: $(HEADERS)
	doxygen Doxyfile

doc-userinstall: doc
	cp -R doc/man/ $(LOCALDIR)

clean:
	$(RM) $(OBJPREFIX)/sgu_static.o
	$(RM) $(OBJPREFIX)/sgu_dynamic.o
	$(RM) $(OBJECTS)
	$(RM) $(SOTARGET)
	$(RM) $(ATARGET)
	$(RM) -r ./doc/

	$(MAKE) -C test clean


